﻿using UnityEngine;
using System.Collections;

public class DetectionUpdate : MonoBehaviour {

	public Texture touchTexture;
	public Texture patternTexture;
	public Texture patternTexture1;
	public Texture patternTexture2;
	public Texture patternTexture3;
	public Texture coneTexture;
	
	private int guiScale;
	private Matrix4x4 guiMatrix;
	public GUIStyle nameStyle; 
	public string namePerso; 
	public VPattern pat;
	
	public GameObject FirePlayer;
	public GameObject WaterPlayer;
	public GameObject AirPlayer;
	
	public GameObject FireBolt;
	public GameObject WaterBolt;
	public GameObject AirBolt;
	public GameObject cloneFire;
	public GameObject cloneFire2;
	public GameObject cloneAir;
	public GameObject cloneWater;
	
	
	//public bool single;
	int nbGOScene;
	bool AirFiring = false;
	bool FireFiring = false;
	bool WaterFiring = false;

	public float cooldown = 1;
	public float shootTimer;

	int patTop;
	int patLeft;
	int patRight;

	void Awake()
	{
		guiScale = Screen.height / 1024;
		if(guiScale == 0) {
			guiScale=1;
		}
		guiMatrix = Matrix4x4.Scale(new Vector3(guiScale, guiScale, 1));
		print("guiScale"+guiScale);
	}
	
	void Start () 
	{
		//single = true;
		VPatternDetector.SetDistanceTolerance(2.0f);
		
		if(!VPatternDetector.RegisterPattern(23, new Vector2(0.0f, 12.5f), 3, new Vector2(0, 7)))print ("Can't register pattern 3");
		
		if(!VPatternDetector.RegisterPattern(15, new Vector2(0, 21.5f), 4, new Vector2(0, 10)))print ("Can't register pattern 4");
		
		if(!VPatternDetector.RegisterPattern(22, new Vector2(0.0f, 15.75f), 5, new Vector2(0, 7)))print ("Can't register pattern 5");
	}

	
	// Update is called once per frame
	void Update () {

		GameObject[] allGameobjects = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[]; 
		nbGOScene = allGameobjects.Length;
	
		if (!VPatternDetector.IsSupported()) {
			GUI.Label(new Rect(20, 20, 600, 40), "pattern detection not supported on this platform", nameStyle);
		}
		else {
			// display PPI
			//GUI.Label(new Rect(20, 60, 600, 40), "unity ppi="+Screen.dpi+" width="+Screen.width + "height="+Screen.height, nameStyle);
		}
		
		// Detected how many patterns
		int patCount = VPatternDetector.DetectPatterns();
		
		// display touches
		int screenHeight 	= Screen.height/guiScale;
		int touchWidth		= touchTexture.width;
		int touchHeight		= touchTexture.height;
		int touchCount		= Input.touchCount;
		
		/*
		for (int i = 0; i < touchCount; ++i) {
			Touch touch = Input.GetTouch(i);
			if (touch.phase!=TouchPhase.Ended && touch.phase!=TouchPhase.Canceled) {
				print ("touched");
				Vector2 pos = new Vector2(touch.position.x/guiScale, touch.position.y/guiScale);
				pos.y = screenHeight - pos.y;
				
				Vector3 PosP = new Vector3(touch.position.x, touch.position.y, 25);
				
				// real position
				GUI.DrawTexture(new Rect(pos.x - touchWidth/2, pos.y - touchHeight/2, touchWidth, touchHeight), touchTexture);

				/*	if(touch.phase == TouchPhase.Began )
							{
					clone = Instantiate(FireBolt, Camera.main.ScreenToWorldPoint(PosP), Quaternion.identity) as GameObject;
							clone.rigidbody2D.AddForce(pat.direction);
				}
			}
		}
		*/
		
		// display detected patterns
		/*int patWidth		= patternTexture.width;
		int patHeight		= patternTexture.height;
		int coneWidth		= coneTexture.width;
		int coneHeight		= coneTexture.height;*/
		Matrix4x4 matrixBackup;
		Vector2 dirRef		= new Vector2(0, 1);
		
		for (int patIndex = 0; patIndex < patCount; ++patIndex) 
		{		
				// detect current pattern
				pat = VPatternDetector.GetDetectedPattern (patIndex);

				Vector2 pos = new Vector2 (pat.position.x / guiScale, pat.position.y / guiScale);
				pos.y = screenHeight - pos.y;
				
				// find direction	
				Vector2 dir = pat.direction;
				float angle = AngleSigned (dir, dirRef);
				
				// transform position
				Vector3 posP = new Vector3(pat.position.x, pat.position.y,25);
				// real position
				// pattern
				// try
				Touch touch = Input.GetTouch(patIndex);
				// try
				
				// add offset for firing position
				Vector3 decal = new Vector3 (pat.direction.x *1.25f , pat.direction.y*1.25f, 25);


				// if current pattern == Fire
				if(pat.userData==3)
				{
					namePerso = "Vulco";
				//	patternTexture = patternTexture1;
					FirePlayer.transform.position=Camera.main.ScreenToWorldPoint(posP);
					if(touch.phase == TouchPhase.Began)
					{
					//	if(!GameObject.FindWithTag("Fire_Bolt"))
					//	{
						//if(!FireFiring)
						//{
							cloneFire = Instantiate(FireBolt, FirePlayer.transform.position + decal, Quaternion.identity) as GameObject;
							cloneFire.rigidbody2D.AddForce(pat.direction*100);
							break;
							//FireFiring = true;
						//}	
					
					}

					
				}
				// if current pattern == Fire
				if(pat.userData==4)
				{ 
					patLeft = pat.touchLeftID;
					patRight = pat.touchRightID;
					patTop = pat.touchFrontID;
					namePerso = "Heriw";
				//	patternTexture = patternTexture2;
					AirPlayer.transform.position=Camera.main.ScreenToWorldPoint(posP);
					if(touch.phase == TouchPhase.Began )
					{
						//	if(!GameObject.FindWithTag("Air_Bolt"))
						//	{
						//if(!AirFiring)
						//{
							cloneAir = Instantiate(AirBolt, AirPlayer.transform.position + decal, Quaternion.identity) as GameObject;
							cloneAir.rigidbody2D.AddForce(pat.direction*100);
							break;
					//	}
					}

				}

				// if current pattern == Fire
				if(pat.userData==5)
				{
					namePerso = "Tsuna";
				//	patternTexture = patternTexture3;
					WaterPlayer.transform.position=Camera.main.ScreenToWorldPoint(posP);
					if(touch.phase == TouchPhase.Began )
					{
						//if(!GameObject.FindWithTag("Water_Bolt"))
						//	{
						//if(!WaterFiring)
						//{
						cloneWater = Instantiate(WaterBolt, WaterPlayer.transform.position + decal, Quaternion.identity) as GameObject;
						cloneWater.rigidbody2D.AddForce(pat.direction*100);
						break;
						//}
						
					}

				}
				//GUI.DrawTexture (new Rect (pos.x - patWidth / 2, pos.y - patHeight / 2, patWidth, patHeight), patternTexture);
				
				// cone
				matrixBackup = GUI.matrix;
				GUIUtility.RotateAroundPivot (angle, new Vector2 (pos.x * guiScale, pos.y * guiScale));	
				//GUI.DrawTexture (new Rect (pos.x - coneWidth / 2, pos.y - coneHeight, coneWidth, coneHeight), coneTexture);
				GUI.matrix = matrixBackup;
				
				
				
				// pattern
				//GUI.DrawTexture (new Rect (pos.x - patWidth / 2, pos.y - patHeight / 2, patWidth, patHeight), patternTexture);
				
				
				switch (pat.userData) {
					
				case 3 : 
					namePerso = "Vulco";
					break;
					
				case 4 : 
					namePerso = "Heriw";
					break;
					
				case 5 :
					namePerso = "Tsuna";
					break;
					
				}
				
				
				// label
				//GUI.Label(new Rect(pos.x+200, pos.y-50, 200, 50), namePerso, nameStyle);
			
		}
		
		//GUI.Label(new Rect(pos.x+200, pos.y-50, 200, 50), "\ndirection: "+pat.direction , nameStyle);
		//GUI.Label(new Rect(200, 50, 200, 50), "\nnombre d'objets: "+nbGOScene , nameStyle);
		//GUI.Label(new Rect(200, 50, 200, 50), "\n\nfiring = "+ firing , nameStyle);
	}

		void OnGUI()
	{/*


		
		GUIUtility.ScaleAroundPivot(new Vector2(guiScale, guiScale), new Vector2(Screen.width/2,Screen.height/2)); 

		GUI.Label(new Rect(200, 50, 200, 50), "\nnombre d'objets: "+nbGOScene , nameStyle);
		//GUI.Label(new Rect(200, 50, 200, 50), "\n\nfiring = "+ firing , nameStyle);

		GUI.Label(new Rect(200, 50, 200, 50), "\n\n\n"+FirePlayer.transform.position.x +"\npatLeft= "+patLeft+"\npatRight= "+patRight+"\npatTop= "+patTop, nameStyle);
		
		Debug.Log ("pos Vulco X = " + FirePlayer.transform.position.x + " , Y = " + FirePlayer.transform.position.y );
		Debug.Log ("pos Heriw X = " + AirPlayer.transform.position.x + " , Y = " + AirPlayer.transform.position.y );
		Debug.Log ("pos Tsuna X = " + WaterPlayer.transform.position.x + " , Y = " + WaterPlayer.transform.position.y );

		Vector3 posCam= Camera.main.WorldToScreenPoint (FirePlayer.transform.position);
		//float y = Camera.main.ScreenToWorldPoint (FirePlayer.transform.position.y);

		GUI.Label(new Rect(posCam.x,-posCam.y, 200, 50), "Vulco", nameStyle);
		GUI.Label(new Rect(AirPlayer.transform.position.x+200, AirPlayer.transform.position.y-50, 200, 50), "Heriw", nameStyle);
		GUI.Label(new Rect(WaterPlayer.transform.position.x+200, WaterPlayer.transform.position.y-50, 200, 50), "Tsuna", nameStyle);
*/
}
	
	float AngleSigned(Vector2 v1, Vector2 v2) 
	{
		v1.Normalize();
		v2.Normalize();
		float angle = Mathf.Atan2(v1.x * v2.y - v1.y * v2.x, Vector2.Dot(v1, v2));
		if (Mathf.Abs(angle) < Mathf.Epsilon) {
			return 0;
		}
		return angle * Mathf.Rad2Deg;	
	}	
}
